﻿<!-- Views/Admin/ImportEmployees.cshtml -->
@{
    ViewBag.Title = "导入员工";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container">
    <h2>导入员工</h2>
    <hr />

    <!-- 显示消息 -->
    @if (TempData["SuccessMessage"] != null)
    {
        <div class="alert alert-success alert-dismissible" role="alert">
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            <strong>成功！</strong> @Html.Raw(TempData["SuccessMessage"])
        </div>
    }

    @if (ViewBag.ErrorMessage != null)
    {
        <div class="alert alert-danger">
            <strong>错误！</strong> @Html.Raw(ViewBag.ErrorMessage)
        </div>
    }

    @if (ViewBag.WarningMessage != null)
    {
        <div class="alert alert-warning">
            <strong>警告！</strong> @Html.Raw(ViewBag.WarningMessage)
        </div>
    }

    <div class="panel panel-default">
        <div class="panel-heading">
            <h3 class="panel-title">导入说明</h3>
        </div>
        <div class="panel-body">
            <h4>模板格式说明：</h4>
            <ul>
                <li><strong>用户名</strong>：必填，员工登录系统的用户名</li>
                <li><strong>姓名</strong>：必填，员工的真实姓名</li>
                <li><strong>角色</strong>：必填，只能填写 Employee</li>
                <li><strong>密码</strong>：必填，员工登录密码</li>
            </ul>

            <div class="alert alert-info">
                <h4>重要提示：</h4>
                <ul>
                    <li>模板中的示例行（第3行）将被自动过滤，不会导入</li>
                    <li>请从第4行开始填写您的员工数据</li>
                    <li>空行将被自动跳过</li>
                    <li>系统会自动验证数据格式和完整性</li>
                    <li>已存在的用户名将被跳过</li>
                </ul>
            </div>

            <h4>使用步骤：</h4>
            <ol>
                <li>点击下方"下载导入模板"按钮下载Excel模板</li>
                <li>从第4行开始填写员工数据（前3行是示例，会被自动过滤）</li>
                <li>保存Excel文件</li>
                <li>在本页面选择该文件并点击"导入员工"</li>
            </ol>

            <p>
                <button type="button" id="downloadTemplate" class="btn btn-info">
                    <i class="glyphicon glyphicon-download"></i> 下载导入模板
                </button>
            </p>
        </div>
    </div>

    @using (Html.BeginForm("ImportEmployees", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()

        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">上传员工文件</h3>
            </div>
            <div class="panel-body">
                <div class="form-group">
                    <label for="excelFile" class="control-label">选择Excel文件</label>
                    <input type="file" class="form-control" id="excelFile" name="excelFile" accept=".xlsx,.xls" />
                    <p class="help-block">支持 .xlsx 和 .xls 格式的Excel文件，文件大小不超过50MB</p>
                </div>

                <div class="form-group">
                    <button type="submit" class="btn btn-primary">
                        <i class="glyphicon glyphicon-upload"></i> 导入员工
                    </button>
                    <a href="@Url.Action("Users", "Admin")" class="btn btn-default">
                        <i class="glyphicon glyphicon-arrow-left"></i> 返回用户管理
                    </a>
                </div>
            </div>
        </div>
    }
</div>

@section Scripts {
    <script>
        $(document).ready(function() {
            // 异步下载模板
            $('#downloadTemplate').click(function() {
                var downloadUrl = '@Url.Action("DownloadEmployeeTemplate", "Admin")';

                // 使用XMLHttpRequest下载文件
                var xhr = new XMLHttpRequest();
                xhr.open('GET', downloadUrl, true);
                xhr.responseType = 'blob';

                xhr.onload = function() {
                    if (xhr.status === 200) {
                        // 创建下载链接
                        var blob = xhr.response;
                        var downloadLink = document.createElement('a');
                        var url = window.URL.createObjectURL(blob);
                        downloadLink.href = url;
                        downloadLink.download = '员工导入模板.xlsx';

                        // 触发下载
                        document.body.appendChild(downloadLink);
                        downloadLink.click();
                        document.body.removeChild(downloadLink);
                        window.URL.revokeObjectURL(url);

                        // 显示下载提示
                        alert('模板下载已开始，请查看浏览器下载列表。');
                    } else {
                        alert('下载模板失败，请重试。');
                    }
                };

                xhr.onerror = function() {
                    alert('下载模板失败，请检查网络连接。');
                };

                xhr.send();
            });

            // 表单提交验证
            $('form').submit(function(e) {
                var fileInput = $('#excelFile');
                if (fileInput.val() === '') {
                    alert('请选择要上传的Excel文件。');
                    e.preventDefault();
                    return false;
                }

                // 检查文件扩展名
                var fileName = fileInput.val().toLowerCase();
                if (!fileName.endsWith('.xlsx') && !fileName.endsWith('.xls')) {
                    alert('请选择.xlsx或.xls格式的Excel文件。');
                    e.preventDefault();
                    return false;
                }

                // 显示处理提示
                $(this).find('button[type=submit]').prop('disabled', true).html('<i class="glyphicon glyphicon-hourglass"></i> 正在导入...');

                return true;
            });
        });
    </script>
}